package com.cuit.riskassessment.action.isms;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.cuit.riskassessment.service.AreaService;
import com.cuit.riskassessment.service.DynManageService;
import com.cuit.riskassessment.service.FocManageService;
import com.cuit.riskassessment.service.NewsManageService;
import com.cuit.riskassessment.util.JsonUtil;
import com.cuit.riskassessment.util.Result;
import com.cuit.riskassessment.util.Tree;
import com.cuit.riskassessment.vo.Area;
import com.cuit.riskassessment.vo.FocManage;
import com.cuit.riskassessment.vo.News;

/**
 * Area 操作的相关action
 * 
 * @author Yang_Zhiqin-pc
 * 
 */
@Controller
@RequestMapping(value = "dyn")
public class DynManageAction {
	final static private String SUCCESS = "success";
	final static private String MESSAGE = "message";
	@Autowired
	@Resource(name = "dynService")
	private DynManageService dynService;
	private Map<String, Object> res;

	public DynManageAction() {
		super();
		res = new HashMap<String, Object>();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody
	Object addFile(HttpServletResponse response, HttpServletRequest request,
			@RequestParam("title") String title, // 设置请求参数的名称和类型
			@RequestParam("content") String content) { // 请求参数一定要与form中的参数名对应
		News news = new News();
		news.setTitle(title);
		news.setContent(content);

		res.clear();
		res.put(MESSAGE, "操作失败！");
		res.put(SUCCESS, false);
		if (this.dynService.add(news)) {
			res.put(MESSAGE, "添加成功");
			res.put(SUCCESS, true);
		}
		return res;
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	Object list(HttpServletResponse response,
			@RequestParam(value = "start") Integer start,
			@RequestParam(value = "limit") Integer limit) {
		response.setHeader("Accept", "application/json");
		res.clear();
		res.put("message", "数据读取错误，可能没开服务器哦");
		res.put("success", false);
		List<News> list = dynService.page(start, limit);
		if (list != null) {
			res.put("success", true);
			res.put("root", list);
			res.put("totalCount", dynService.getTotalCount());
			res.put("message", "读取成功");
		}
		return res;
	}
	@RequestMapping(value = "/del")
	public @ResponseBody
	Object del(@RequestParam(value = "id") int[] ids) {
		dynService.del(ids);
		res.clear();
		res.put("success", true);
		res.put("message", "删除成功");
		return res;
	}
	@RequestMapping(value = "/edit")
	public @ResponseBody
	Object del(News news) {
		res.clear();
		res.put("success", false);
		res.put("message", "删除失败");
		if (dynService.update(news)) {
			res.put("success", true);
			res.put("message", "删除成功");
		}
		return res;
	}
	
}
